import time

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

class CommonDriver:
    def get_driver(self):
        desc = {
            'platformName': 'Android',
            'platformVersion': '7',
            'appPackage': 'com.dangdang.buy2',
            'appActivity': 'com.dangdang.buy2.StartupActivity',
            'noReset': False,
            'unicodeKeyboard': True,
            'udid': '127.0.0.1:21513',
        }

        command_executor_url = 'http://127.0.0.1:4723/wd/hub'

        driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    time.sleep(6)
    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/research_flipper_textview"]').click()
    driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys('浪潮之巅')
    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_search"]').click()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/product_title_tv" and @text="浪潮之巅 第四4版上下两册吴军数学 之美文明之光大学之路见识态度全"]').click()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.FrameLayout[@resource-id="com.dangdang.buy2:id/fr_service_root"]').click()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.RelativeLayout[@resource-id="com.dangdang.buy2:id/rl_btn_left"]').click()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/cart_balance_tv"]').click()


